package cn.com.hbhgkj.bussiness;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.BeanUtils;

import cn.com.hbhgkj.common.DaoException;
import cn.com.hbhgkj.common.Pagination;
import cn.com.hbhgkj.common.ServiceException;
import cn.com.hbhgkj.config.Constants;
import cn.com.hbhgkj.dao.CopyrightApplicationDao;
import cn.com.hbhgkj.dao.CopyrightRegisterationDao;
import cn.com.hbhgkj.pojo.CopyrightApplication;
import cn.com.hbhgkj.pojo.CopyrightRegisteration;
import cn.com.hbhgkj.pojo.User;
import cn.com.hbhgkj.utils.CopyrightSender;
import cn.com.hbhgkj.utils.NameValuePairsFactory;

public class CopyrightApplicationServiceImpl implements
		CopyrightApplicationService {

	public CopyrightApplication loadById(long id) throws ServiceException {
		try {
			return copyrightApplicationDao.get(id);
		} catch (DaoException e) {
			throw new ServiceException(e);
		}
	}

	public List<CopyrightApplication> findByApplicant(User applicant,
			Pagination pagination) throws ServiceException {
		try {
			return copyrightApplicationDao.findByApplicant(applicant,
					pagination);
		} catch (DaoException e) {
			throw new ServiceException(e);
		}
	}

	public List<CopyrightApplication> getPendingReviews(Pagination pagination)
			throws ServiceException {
		try {
			return copyrightApplicationDao.findByReviewCode(
					ReviewCode.PENDING_REVIEW, pagination);
		} catch (DaoException e) {
			throw new ServiceException(e);
		}
	}

	public void submit(CopyrightApplication copyright) throws ServiceException {
		try {

			copyright.setState(ReviewCode.PENDING_REVIEW.getValue());
			Date curDate = new Date();
			copyright.setUpdateTime(curDate);
			copyright.setApplyTime(curDate);
			copyright.setLoginId(Constants.AGENT_USER_NAME);
			/*
			 * if (copyright.getId() != null) { CopyrightApplication ca =
			 * copyrightApplicationDao.get(copyright .getId());
			 * PropertyUtils.copyProperties(ca, copyright); } else {
			 */
			copyrightApplicationDao.saveOrUpdate(copyright);
			// }
		} catch (Exception e) {
			throw new ServiceException(e);
		}
	}

	public void review(User reviwer, CopyrightApplication copyright)
			throws ServiceException {
		try {

			// TODO validate reviewer if is valid
			CopyrightApplication entity = copyrightApplicationDao.get(copyright
					.getId());
			
			//审核人更改
			String[] ignoreCopy = new String[]{"applicant","applyTime","loginId"};
			BeanUtils.copyProperties(copyright, entity,ignoreCopy);
			
			entity.setState(copyright.getState());
			entity.setReviwer(reviwer);
			entity.setUpdateTime(new Date());

			//如果审核失败，不发送
			if(!ReviewCode.isPass(copyright.getState()))
				return;
			
			// send form to copyright center
			CopyrightSender.login(Constants.AGENT_USER_NAME,
					Constants.AGENT_PASSWORD);

			NameValuePairsFactory paramsFactory = new NameValuePairsFactory(
					entity);

			// 发送表单至copyright中心
			// get serial number
			//String serialNum = CopyrightSender.registCopyRight(entity);
			String serialNum = CopyrightSender.send(
					paramsFactory.createNameValuePairs(),
					CopyrightSender.regPostUrl);

			CopyrightRegisteration cr = buildCopyrightRegisteration(serialNum,
					entity);
			// 保存我的登记
			copyrightRegisterationDao.save(cr);

		} catch (Exception e) {
			throw new ServiceException(e);
		}
	}

	private CopyrightRegisteration buildCopyrightRegisteration(
			String serialNum, CopyrightApplication copyright) {
		CopyrightRegisteration cr = new CopyrightRegisteration();
		cr.setApplyFormId(copyright.getId());
		cr.setApplyFormType(ApplicationFormType.R11.getValue());
		cr.setContact(copyright.getSqrcontactPerson());
		cr.setCopyrighter(copyright.getName1());
		cr.setEmail(copyright.getSqremail());
		cr.setPhoneNum(copyright.getSqrphone());
		cr.setProductName(copyright.getSoftFullName());
		cr.setMobileNum(copyright.getSqrmobile());
		cr.setSerialNum(serialNum);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		cr.setRegistDate(sdf.format(new Date()));
		return cr;
	}

	public List<CopyrightApplication> getReviwed(Pagination pagination)
			throws ServiceException {
		try {
			return copyrightApplicationDao.findByReviewCodes(new ReviewCode[] {
					ReviewCode.FAIL, ReviewCode.PASS }, pagination);
		} catch (DaoException e) {
			throw new ServiceException(e);
		}
	}

	private CopyrightApplicationDao copyrightApplicationDao;
	private CopyrightRegisterationDao copyrightRegisterationDao;

	public CopyrightApplicationDao getCopyrightApplicationDao() {
		return copyrightApplicationDao;
	}

	public CopyrightRegisterationDao getCopyrightRegisterationDao() {
		return copyrightRegisterationDao;
	}

	public void setCopyrightRegisterationDao(
			CopyrightRegisterationDao copyrightRegisterationDao) {
		this.copyrightRegisterationDao = copyrightRegisterationDao;
	}

	public void setCopyrightApplicationDao(
			CopyrightApplicationDao copyrightApplicationDao) {
		this.copyrightApplicationDao = copyrightApplicationDao;
	}

}
